import request from './request'
import type { Product } from './product'

// 订单状态枚举（与后端保持一致）
export enum OrderStatus {
  PENDING_PAYMENT = 1,    // 待付款
  PENDING_SHIPMENT = 2,   // 待发货
  PENDING_RECEIPT = 3,    // 待收货
  COMPLETED = 4,          // 已完成
  CANCELLED = 5,          // 已取消
  REFUNDING = 6           // 退款中
}

// 支付状态枚举
export enum PaymentStatus {
  UNPAID = 0,    // 未支付
  PAID = 1       // 已支付
}

// 支付方式枚举
export enum PaymentMethod {
  ALIPAY = 1,      // 支付宝
  WECHAT = 2,      // 微信
  BANK_CARD = 3    // 银行卡
}

// 订单项
export interface OrderItem {
  id: number
  orderId: number
  productId: number
  skuId?: number
  productName: string
  productImage?: string
  skuSpecifications?: any
  price: number
  quantity: number
  totalAmount: number
  createdAt: string
}

// 订单信息
export interface Order {
  id: number
  orderNo: string
  userId: number
  orderStatus: OrderStatus
  paymentStatus: PaymentStatus
  shippingStatus: number
  consigneeName: string
  consigneePhone: string
  consigneeAddress: string
  totalAmount: number
  goodsAmount: number
  shippingFee: number
  pointsAmount: number
  paymentMethod?: PaymentMethod
  paymentTime?: string
  shippingTime?: string
  confirmTime?: string
  deliveryCompany?: string
  deliveryNo?: string
  buyerMessage?: string
  sellerRemark?: string
  autoConfirmDay: number
  createdAt: string
  updatedAt: string
  items: OrderItem[]
  user?: {
    id: number
    username: string
    phone?: string
    email?: string
  }
}

// 创建订单请求
export interface CreateOrderRequest {
  userId: number
  consigneeName: string
  consigneePhone: string
  consigneeAddress: string
  shippingFee?: number
  pointsAmount?: number
  buyerMessage?: string
  items: {
    productId: number
    skuId?: number
    quantity: number
    price: number
  }[]
}

// 订单查询参数
export interface OrderQuery {
  page?: number
  limit?: number
  orderNo?: string
  userId?: number
  orderStatus?: OrderStatus
  paymentStatus?: PaymentStatus
  shippingStatus?: number
  startDate?: string
  endDate?: string
  consigneeName?: string
  consigneePhone?: string
  sort?: string
  order?: 'ASC' | 'DESC'
}

export const orderApi = {
  // 创建订单
  createOrder: (data: CreateOrderRequest) => {
    return request.post<{ orderId: number; orderNo: string }>('/api/orders', data)
  },

  // 获取用户订单列表
  getOrders: (params?: OrderQuery) => {
    return request.get<{
      list: Order[]
      total: number
      page: number
      limit: number
      totalPages: number
    }>('/api/orders', { params })
  },

  // 获取订单详情
  getOrderById: (id: number) => {
    return request.get<Order>(`/api/orders/${id}`)
  },

  // 根据订单号获取订单详情
  getOrderByNo: (orderNo: string) => {
    return request.get<Order>(`/api/orders/order-no/${orderNo}`)
  },

  // 取消订单
  cancelOrder: (id: number) => {
    return request.post(`/api/orders/${id}/cancel`)
  },

  // 确认收货
  confirmOrder: (id: number) => {
    return request.post(`/api/orders/${id}/confirm`)
  },

  // 支付订单
  payOrder: (id: number, paymentMethod: PaymentMethod) => {
    return request.post(`/api/orders/${id}/pay`, { paymentMethod })
  },

  // 获取订单统计
  getOrderStats: () => {
    return request.get('/api/orders/stats')
  }
}